﻿using PortfolioApp.DataModels;
using PortfolioApp.Web.DbAdapters;
using PortfolioApp.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PortfolioApp.Web.Controllers
{
    public class MVCController : Controller
    {
        private IPortfolioAppAdapter _adapter = new PortfolioAppAdapter();

        // GET: MVC
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Posts(int id)
        {
            PostIndexVM vm = new PostIndexVM(id);
            _adapter.GetPostsForPage(vm);


            return View(vm);
        }

        public ActionResult PostDetail(int id)
        {
            PostDetailVM vm = new PostDetailVM();
            vm.Post = _adapter.GetPost(id);
            vm.UserName = _adapter.GetUserName(vm.Post.ApplicationUserId);
            return View(vm);
        }

        public ActionResult PostEdit(int id)
        {
            PostDetailVM vm = new PostDetailVM();
            vm.Post = _adapter.GetPost(id);
            vm.UserName = _adapter.GetUserName(vm.Post.ApplicationUserId);
            return View(vm);
        }
        [HttpPost]
        public ActionResult PostEdit(Post post)
        {
            post = _adapter.UpdatePost(post);

            return RedirectToAction("PostDetail/" + post.Id);
        }

        public ActionResult PostAdd()
        {
            PostDetailVM vm = new PostDetailVM();
            vm.Post = new Post("x", "", "");
            vm.UserName = "blank";
            return View("PostEdit",vm);
        }
        [HttpPost]
        public ActionResult PostAdd(Post post)
        {
            // Manually setting user Id until Auth is working
            post.ApplicationUserId = "40ba2bd1-c737-4d60-ae63-dc283a7d640b";
            post = _adapter.CreatePost(post);
            return RedirectToAction("PostDetail/" + post.Id);
        }

        [HttpPost]
        public ActionResult DeletePost(int id)
        {
            Post post = _adapter.GetPost(id);
            _adapter.DeletePost(post);
            return RedirectToAction("Index");
        }
    }
}